package com.capacitydorm.server.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.capacitydorm.server.common.vo.DataGridBean;
import com.capacitydorm.server.common.vo.PagesVo;
import com.capacitydorm.server.common.vo.Result;
import com.capacitydorm.server.common.vo.StudentVo;
import com.capacitydorm.server.domain.Dorm;
import com.capacitydorm.server.domain.Student;
import com.capacitydorm.server.service.DormService;
import com.capacitydorm.server.service.StudentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/stu")
public class StudentController {

    @Resource
    private StudentService  studentService;


    /**
     * 分页查询所有
     * @param page
     * @param rows
     * @return
     */
    @GetMapping
    @RequestMapping("/page")
    public ResponseEntity<Result>   pageFindStudent(Integer  page,Integer  rows){
        try {
            PageInfo<Student> info = studentService.pageFindStudent(page, rows);
            DataGridBean  dataGridBean=new DataGridBean();
            dataGridBean.setTotal(info.getTotal());
            dataGridBean.setRows(info.getList());
            String jsonString = JSON.toJSONString(dataGridBean, SerializerFeature.DisableCircularReferenceDetect);
          return    new ResponseEntity<>(new Result(200,null,JSON.parse(jsonString)),HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return   new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 批量删除学生
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteStudents/{ids}")
    public    ResponseEntity<Result>   deleteStudents(@PathVariable("ids")String  ids){
          try{
              System.out.println(ids);
              String[] split = ids.split(",");
              for (String   id:split){
                  studentService.deleteStudent(id);
              }
              return    new ResponseEntity<>(new  Result(204),HttpStatus.NO_CONTENT);
          }catch (Exception  e){
              e.printStackTrace();
              return   new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
          }
    }
    /**
     * 删除学生
     * @param id
     * @return
     */
    @DeleteMapping("/deleteStudent/{id}")
    public    ResponseEntity<Result>   deleteStudent(@PathVariable("id")String  id){
        try{
            studentService.deleteStudent(id);
            return    new ResponseEntity<>(new  Result(204),HttpStatus.NO_CONTENT);
        }catch (Exception  e){
            e.printStackTrace();
            return   new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }
    /**
     * 根据ID查找学生
     */
      @GetMapping("/findStudentById")
     public  ResponseEntity<Result>   findStudentById(String  id){
          try {
              Student student = studentService.findById(id);
              if (student==null){
                  return   new ResponseEntity<>(HttpStatus.NOT_FOUND);
              }else {
                  return   new ResponseEntity<>(new Result(200,null,student),HttpStatus.OK);
              }
          } catch (Exception e) {
              e.printStackTrace();
              return    new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
          }

        }

    /**
     * 根据ID修改学生状态
     * @param student
     * @return
     */
    @PutMapping
    @RequestMapping("/editStudent")
    public ResponseEntity<Result>   editStudent(Student  student){
        try {
            System.out.println(student.getId());
            Student byId = studentService.findById(student.getId()+"");
            if (byId==null){
                return    new ResponseEntity<>( new Result(404),HttpStatus.NOT_FOUND);
            }else {
                studentService.editStudent(student);
                return   new ResponseEntity<>(new Result(204),HttpStatus.NO_CONTENT);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return    new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 根据ID修改学生在寝状态
     * @param student
     * @return
     *
     */
    @PutMapping("/editStater")
    public ResponseEntity<Result>   editStudentstate(Student  student){

        try {
            //添加代码
            studentService.editStudentState(student);
            return new ResponseEntity<>( new Result(201),HttpStatus.CREATED);
        } catch (Exception e) {
            e.printStackTrace();
            return    new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    /**
     * 根据ID查询学生
     * @param id
     * @return
     */
    @GetMapping
    @RequestMapping("/findById")
    public    ResponseEntity<Result>   findById(String  id){
        try {
            Student student = studentService.findById(id);
            return   new ResponseEntity<>(new Result(200,"student",student),HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return    new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 添加学生
     * @param student
     * @return
     */
    @Autowired
    private DormService  dormService;
    @PostMapping
    public     ResponseEntity<Result>   addStudent(Student  student){
        try {
            Dorm dorm = dormService.checkDormById(student.getDormId());
            List<Student> students = dorm.getStudents();
            if (students.size()<4){
                student.setLastout(new Date());
                student.setLastin(new Date());
                studentService.addStudent(student);
                return   new ResponseEntity<>(new  Result(201),HttpStatus.CREATED);
            }
            return    new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            return    new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 根据多条件查询学生
     * @param page
     * @param rows
     * @param student
     * @return
     */
    @GetMapping("/search")
    public     ResponseEntity<Result>   findStudent(Integer page,Integer  rows,Student  student){
        try {
            PageInfo<Student> byStudent = studentService.findByStudent(page, rows, student);
            System.out.println(byStudent.getTotal());
            String jsonString = JSON.toJSONString(new DataGridBean(byStudent.getTotal(),byStudent.getList()), SerializerFeature.DisableCircularReferenceDetect);
            return   new ResponseEntity<>(new  Result(201,null,JSON.parse(jsonString)),HttpStatus.CREATED);
        } catch (Exception e) {
            e.printStackTrace();
            return    new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 根据姓名模糊查询不要总条数
     * @param page
     * @param rows
     * @param name
     * @return
     */
    @GetMapping("/name")
    public     ResponseEntity<Result>   findStudentByName(Integer page,Integer  rows,String  name) {
        try {
            PageInfo<Student> byStudent = studentService.findStudentByName(page, rows, name);
            String jsonString = JSON.toJSONString(new PagesVo(byStudent.getPages(),byStudent.getList()), SerializerFeature.DisableCircularReferenceDetect);
            return new ResponseEntity<>(new Result(201, null, JSON.parse(jsonString)), HttpStatus.CREATED);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 根据姓名模糊查询总条数
     * @param page
     * @param rows
     * @param name
     * @return
     */
    @GetMapping("/nameTotal")
    public     ResponseEntity<Result>   findStudentByNameTotal(Integer page,Integer  rows,String  name) {
        try {
            PageInfo<Student> byStudent = studentService.findStudentByName(page, rows, name);
            String jsonString = JSON.toJSONString(new DataGridBean(byStudent.getTotal(),byStudent.getList()), SerializerFeature.DisableCircularReferenceDetect);
            return new ResponseEntity<>(new Result(201, null, JSON.parse(jsonString)), HttpStatus.CREATED);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

}
